package org.exhaust.framework.net.cmpp20.protocol;

import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.exhaust.framework.net.common.protocol.BaseFrame;
import org.exhaust.framework.net.common.protocol.FrameHeader;
@Slf4j
@Data
public class CMPPMessage extends BaseFrame {
    private CMPPHeader header;
    private CMPPBody body;

    @Override
    protected void encodeData(ByteBuf out) {
        header.encodeData(out);
        body.encode(out);
    }

    @Override
    public void decode(ByteBuf in, FrameHeader header) {
        CMPPHeader cmppHeader = new CMPPHeader();
        cmppHeader.decode(in,null);
        long commandId = cmppHeader.getCommandId();

        CMPPBody body  = CMPPCommandHelper.createBody(commandId);
        if(body == null){
            String error = "未知的命令码：" + commandId;
            log.error(error);
            throw new RuntimeException(error);
        }
        body.decode(in,header);

        this.header = cmppHeader;
        this.body = body;
    }
}
